package com.pgytesting.secondhandcar.util;


import com.pgytesting.secondhandcar.model.AdministratorInformation;
import com.pgytesting.secondhandcar.model.McpCarDealerInformation;
import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.SMSCode;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author : frosty0804@gmail.com
 * @version : v1.0
 * @projectName : SecondHandCar
 * @className : SessionUtil
 * @description : TODO(Session工具类)
 * @date : 2019/7/29 18:58
 */
public class SessionUtil {

    /**
     * 获取request信息
     *
     * @return 获取请求到的request信息
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }


    /**
     * 获取验证码信息
     *
     * @return
     */
    public static SMSCode getSessionSMSCode() {
        return (SMSCode) getSession().getAttribute(Parameters.SMS_CODE);
    }

    /**
     * 获取session中销售信息
     *
     * @return
     */
    public static McpCarDealerInformation getSessionMcpCar() {
        return (McpCarDealerInformation) getSession().getAttribute("user");
    }

    /**
     * 获取session中用户信息
     *
     * @return
     */
    public static McpUserInfo getSessionMcpUser() {
        return (McpUserInfo) getSession().getAttribute("user");
    }

    /**
     * 获取session中管理员信息
     *
     * @return
     */
    public static AdministratorInformation getSessionAdmin() {
        return (AdministratorInformation) getSession().getAttribute("user");
    }

    /**
     * 移除验证码
     *
     * @return
     */
    public static void removeSMSCode() {
        getSession().removeAttribute("SMSCode");
    }


    /**
     * 获取请求中的session信息
     *
     * @return session信息
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 更新，添加session
     *
     * @param kay   需要插入session的key
     * @param value 需要插入session的value
     */
    public static void updateSession(String kay, Object value) {
        getSession().setAttribute(kay, value);
    }


    /**
     * 查询用户类型
     *
     * @return
     */
    public static String getUserType(){
        return String.valueOf(getSession().getAttribute("login"));
    }
}
